<?php
require_once 'models/model_manager.php';
require_once 'models/conflit.php';
require_once 'models/bataille_manager.php';

class conflit_manager extends Manager
{
	protected $_date;
	
	public function __construct($db) {
		parent::__construct($db);
		$this->_date = new date_historique_manager($this->_db);
	}	

	public function get_all() {
		$conflits = array();
		$query = "SELECT * FROM conflit";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		
		while ($data = oci_fetch_assoc($result)) {
			$conflits[] = new conflit($data);
		}
		$this->set_date_debut($conflits);
		$this->set_date_fin($conflits);
		$this->set_batailles($conflits);
		return $conflits;
	}

	public function get_by_id($id){
		$conflit;
		$query = "SELECT * FROM conflit WHERE idConflit = ".$id;
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		while ($data = oci_fetch_assoc($result)) {
			$conflit = new conflit($data);
			$conflit->set_debut($this->get_date_debut($conflit->get_idconflit()));
			$conflit->set_fin($this->get_date_fin($conflit->get_idconflit()));
			$r = new bataille_manager($this->_db);
			$conflit->set_batailles($r->get_all_by_conflit($conflit->get_idconflit()));
		}
		return $conflit;
	}
	
	public function set_date_debut($conflits) {
		foreach ($conflits as $c) {
			$c->set_debut($this->get_date_debut($c->get_idconflit()));
		}
	}
	
	public function set_date_fin($conflits) {
		foreach ($conflits as $c) {
			$c->set_fin($this->get_date_fin($c->get_idconflit()));
		}
	}

	public function set_batailles($conflits) {
		foreach ($conflits as $c) {
			$c->set_batailles(bataille_manager::get_all_by_conflit($c->get_idconflit()));
		}
	}
	
	public function get_date_debut($id) {
		$query =
			"SELECT to_char(\"DATE\", 'DD/MM/SYYYY') FROM date_historique
			WHERE reference = 1
				AND idDate IN (SELECT idDate FROM debut_conflit
								WHERE idConflit = " . $id . ")";
		$result = oci_parse($this->_db, $query);
		
		oci_execute($result);
		$d = oci_fetch_row($result);
		return $d[0];
	}
	
	public function get_date_fin($id) {
		$query =
			"SELECT to_char(\"DATE\", 'DD/MM/SYYYY') FROM date_historique
			WHERE reference = 1
				AND idDate IN (SELECT idDate FROM fin_conflit
								WHERE idConflit = " . $id . ")";
		$result = oci_parse($this->_db, $query);
		
		oci_execute($result);
		$d = oci_fetch_row($result);
		return $d[0];
	}
	
	public function max_id() {
		$query = "SELECT max(idConflit) FROM conflit";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$r = oci_fetch_row($result);
		return $r[0];
	}

	public function add_or_update($e){
		$query = "SELECT COUNT(*) FROM conflit WHERE idConflit=".$e->get_idconflit();
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
		$d = oci_fetch_row($result);
		if($d[0] == 0)$this->add($e);
		else{
			$query = "UPDATE conflit SET nomConflit='".$e->get_nomconflit()."', wikiConflit='".$e->get_wikiconflit()."', mortsConflit=".$e->get_mortsconflit().", blessesConflit=".$e->get_blessesconflit()." WHERE idConflit=".$e->get_idconflit();
			$result = oci_parse($this->_db, $query);
			echo "<br>" . $query . "<br><br>";
			oci_execute($result);
			if($e->get_debut() != ""){
				$this->add_debut($e->get_idconflit(),$e->get_debut());
			}
			if($e->get_fin() != ""){
				$this->add_fin($e->get_idconflit(),$e->get_fin());
			}
			$r = new bataille_manager($this->_db);
			foreach($e->get_batailles() as $b){
				$r->add_or_update($b,$e->get_idconflit());
			}
		}
	}
	
	public function add($e) {
		$query = "INSERT INTO conflit(idConflit, nomConflit, wikiConflit, mortsConflit, blessesConflit)
			VALUES (" . $e->get_idconflit() . ",'" . $e->get_nomconflit() . "','" . $e->get_wikiconflit() . "'," . $e->get_mortsconflit() . "," . $e->get_blessesconflit() . ")";
		$result = oci_parse($this->_db, $query);
		echo "<br>" . $query . "<br><br>";
		oci_execute($result);
		if($e->get_debut() != ""){
			$this->add_debut($e->get_idconflit(),$e->get_debut());
		}
		if($e->get_fin() != ""){
			$this->add_fin($e->get_idconflit(),$e->get_fin());
		}
		$r = new bataille_manager($this->_db);
		foreach($e->get_batailles() as $b){
			$r->add_or_update($b,$e->get_idconflit());
		}
	}

	public function delete($e){
		$query = "DELETE FROM conflit WHERE idConflit =" . $e->get_idconflit();
		$result = oci_parse($this->_db, $query);
		echo "<br>" . $query . "<br><br>";
		oci_execute($result);
	}

	public function add_debut($idConflit, $date) {
		$id = $this->_date->max_id()+1;
		$this->_date->add(new date_historique(array(
								"idDate" => $id, 
								"reference" => 1,
								'date' => $date)));
		$query = "INSERT INTO debut_conflit(idDate, idConflit) VALUES(" . $id . ", " . $idConflit . ")";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
	}
	public function add_fin($idConflit, $date) {
		$id = $this->_date->max_id()+1;
		$this->_date->add(new date_historique(array(
								"idDate" => $id, 
								"reference" => 1,
								'date' => $date)));
		$query = "INSERT INTO fin_conflit(idDate, idConflit) VALUES(" . $id . ", " . $idConflit . ")";
		$result = oci_parse($this->_db, $query);
		oci_execute($result);
	}
}
?>
